var Poker = require('../Poker/Poker');

var PokerPlayer = require('../Poker/player').PokerPlayer;

var Const = require('./const');
var SubCode = require('../subCode');
var Recommend = require('./recommend')
exports.SSZPlayer = PokerPlayer.extend({
    init: function (uid, msg) {
        this._super(uid, msg);
        this.reciveCards = null; //收到的牌型
        this.settleCards = null; //
        this.allScores = {
            'headScore': [0, 0],
            'bodyScore': [0, 0],
            'footerScore': [0, 0],
            'long':[0,0]
            //'gun': []
        };
        this.storeScores = {
            'headScore': [0, 0],
            'bodyScore': [0, 0],
            'footerScore': [0, 0],
            'long':[0,0]
        }
        this.settled = false;
        this._isStandard = true;
        this._stat.roundPattern = Const.Pattern.NONE;

        this.recommendCards = [];
        this.winSum = 0;

    },
    sendHeadScore: function (score) {
        this.allScores['headScore'][0] += score;
    },
    sendBodyScore: function (score) {
        this.allScores['bodyScore'][0] += score;
    },
    sendFootScore: function (score) {
        this.allScores['footerScore'][0] += score;
    },
    sendLongScore:function(score){
        this.allScores['long'][0] += score;
    },
    addHeadScore: function (score) {
        this.storeScores['headScore'][0] += score;
    },
    addBodyScore: function (score) {
        this.storeScores['bodyScore'][0] += score;
    },
    addFootScore: function (score) {
        this.storeScores['footerScore'][0] += score;
    },
    addLongScore:function(score){
        this.storeScores['long'][0] += score;
    },
    // addGun: function (playerIndex, score) {
    //     this.allScores['gun'].push({
    //         'playerIndex': playerIndex,
    //         'score': score
    //     });
    // },
    getSumScore: function () {
        return this.storeScores['headScore'][0] + this.storeScores['bodyScore'][0] + this.storeScores['footerScore'][0]+this.storeScores['long'][0];
    },
    //
    getPrepare: function () {
        return reciveCards;
    },

    //准备好结算
    isPrepare: function () {
        if (this.reciveCards) {
            return true;
        }
        return false;
    },
    setPrepare: function (cards) {

        if (!cards) {
            return false;
        }
        if (cards['head'] && cards['body'] && cards['footer']) {
            this.reciveCards = cards;
            return true;
        }



        // var cards = Poker.buildFromArray(cardsArr);
        // var allExist = true;
        // var allPoint = 0;

        // ExObject.eachKeyNum(cards, function (card, num) {
        //     if (!card) {
        //         allExist = false;
        //     }

        //     if (this._handCards.getCardNum(card) < num) {
        //         allExist = false;
        //     }

        //     allPoint += NN.getPoint(card) * num;
        // }.bind(this));

        // if (!allExist) {
        //     return false;
        // }

        // if (allPoint % NN.BASE != 0) {
        //     return false;
        // }

        // this._selectedCards = [];
        // cardsArr.forEach(function (card) {
        //     this._selectedCards.push(card);
        // }.bind(this));

        // return true;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/
    //初始化发牌13张
    getInitHandCardNeedNum: function () {
        return 13;
    },
    initHandCards: function (cardArr) {
        this._handCards.init(cardArr);
        var TJ = Recommend.recom(this._handCards.getInitCards());
        this.recommendCards = TJ;
        this.sendReplay("onInitHandCards", {
            playerIndex: this.getIndex(),
            cards: this._handCards.getInitCards(),
            remain: this.getRoom().getPublicCards().getRemainNum(),
            recommend: TJ
        });
    },
    getInfo: function () {
        var info = this._super();
        info.remainScore = this._stat.remainScore;
        return info;
    },

    getReconnectInfo: function (isSelf) {
        var info = this._super(isSelf);
        if (isSelf) {
            if (this.reciveCards) {
                if (this.settled) {
                    info.allScores = this.allScores;
                    info.settleCards = this.settleCards;
                }
            } else {
                info.handCards = this._handCards.getInitCards();
                info.recommend = this.recommendCards;
            }
        }
        if (this.settled) {
            info.settleCards = this.settleCards
        }
        info.isSendCards = !!(this.reciveCards);
        info.settled = this.settled;
        info.sumScore = this._stat.score;
        return info;
    },

    getSettlementInfo: function () {
        var info = this._super();
        // console.log('实际得Coin')
        // console.log(this.getRoundCoin());
        // console.log(this.getResource('coin'))
        //this._countCoinActual();
        info.handCards = this._handCards.getInitCards();
        info.reciveCards = this.reciveCards;
        info.sumScore = this._stat.score;
        info.allScores = this.allScores;
        info.isStandard = this._isStandard;
        info.settleCards = this.settleCards;
        info.roundCoin = this.getRoundCoin();
        //info.storeScores = this.storeScores;
        //info.coin = this.getResource('coin');
        return info;
    },
    settlementPattern: function () {
        // if (!this._selectedCards) {
        //     var results = NN.calcNiuCards(this._handCards.getInitCards());
        //     this._selectedCards = (results.length > 0) ? results[0] : [];
        // }

        // this._calcNiu();
    },

    settlement: function () {
        this._super();
        this.settled = true;
        this.addRoundScore(this.getSumScore());
        // var dealer = this.getRoom().getDealerPlayer();
        // if (!dealer) {
        //     return;
        // }

        // if (this.uid() == dealer.uid()) {
        //     return;
        // }

        // if (dealer._stat.roundPattern > this._stat.roundPattern) {
        //     var score = Const.PatternScore[dealer._stat.roundPattern] + 1;
        //     dealer.addRoundScore(score);
        //     this.addRoundScore(-score);
        // } else if (dealer._stat.roundPattern < this._stat.roundPattern) {
        //     var score = Const.PatternScore[this._stat.roundPattern] + 1;
        //     this.addRoundScore(score);
        //     dealer.addRoundScore(-score);
        // } else {
        //     var score = Const.PatternScore[this._stat.roundPattern] + 1;
        //     if (dealer._getMaxCard() > this._getMaxCard()) {
        //         dealer.addRoundScore(score);
        //         this.addRoundScore(-score);
        //     } else {
        //         this.addRoundScore(score);
        //         dealer.addRoundScore(-score);
        //     }
        // }
    },

    onStartNewRound: function () {
        this._super();
        this.reciveCards = null;
        this.allScores = {
            'headScore': [0, 0],
            'bodyScore': [0, 0],
            'footerScore': [0, 0],
            'long':[0,0]
            //'gun': []
        };
        this.storeScores = {
            'headScore': [0, 0],
            'bodyScore': [0, 0],
            'footerScore': [0, 0],
            'long':[0,0]
        }
        this.settleCards = null;
        this._isStandard = true;
        this.winSum = 0;
        this.recommendCards = [];
        this.settled = false;
        //this._stat.roundPattern = Const.Pattern.NONE;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    _calcPattern: function () {

    },
    _calcNiu: function () {
        var selectCards = this._selectedCards;
        if (!selectCards || selectCards.length == 0) {
            this._stat.roundPattern = Const.Pattern.NONE;
            return;
        }

        var allCardsArr = this._handCards.getInitCards();
        var otherPoints = 0;
        allCardsArr.forEach(function (card) {
            if (selectCards.indexOf(card) == -1) {
                otherPoints += NN.getPoint(card);
            }
        });

        var nPoint = otherPoints % NN.BASE;
        var pattern = Const.Pattern.NONE;

        if (nPoint == 0) {
            pattern = Const.Pattern.NN;
        } else {
            pattern = Const.Pattern['N' + nPoint];
        }

        this._stat.roundPattern = pattern;
    },

    _getMaxCard: function () {
        var maxCard = 0;
        var maxPoint
        var selectCards = this._selectedCards;
        var allCardsArr = this._handCards.getInitCards();

        allCardsArr.forEach(function (card) {
            if (selectCards && selectCards.indexOf(card) == -1) {
                if (!maxCard || NN.cardLT(card, maxCard)) {
                    maxCard = card;
                }
            }
        });

        return maxCard;
    },
    getInitHandCardPreCards: function () {

        var preCards = [];
        if (this._prehandcards) {
            for (var i = 0; i < this._prehandcards.length; i++) {
                preCards.push(this._prehandcards[i]);
            }
        }
        // var distance = this.getRoom().getIndexDistance(this.getIndex(), this.getRoom().getDealerIndex());
        // if (distance == 0) {

        // } else if (distance == 1) {

        // }
        var room = this.getRoom();
        logger.debug("room=%d noPreCards %j,cards = %j", room.rid(), preCards, this._prehandcards);

        return preCards;
    },
});